﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETM.Data.DataTransferObjects;
using ETM.Data.Entities;
using ETM.Data.Interfaces;

namespace ETM.Data.Repositories
{

    public class TaskMockupRepository : ITaskRepository
    {
        private static readonly TaskMockupRepository _instance;
        private static IEnumerable<TaskEntity> DataSource;

        private TaskMockupRepository() { }

        static TaskMockupRepository()
        {
            _instance = new TaskMockupRepository();

            var tmpList = new List<TaskEntity>();

            #region Populate mockup data

            var taskNames = new string[] { "Clean the floow", "Brush teeth", "Prepare a mockup data", "Play some games", "Answer email" };
            var taskStatus = new string[] { "Processing" };

            var rnd = new Random(DateTime.Now.Millisecond);

            for (int i = 1; i < 20; i++)
            {
                // The old attributes
                var isUrgent = (rnd.Next(0, 2) == 1);
                var isImportant = (rnd.Next(3, 5) == 4);

                var taskEntity = new TaskEntity("T_" + i.ToString(), taskNames[rnd.Next(0, taskNames.Length)],
                                                taskStatus[rnd.Next(0, taskStatus.Length)], isUrgent, isImportant, DateTime.Now);
                tmpList.Add(taskEntity);

            }

            DataSource = tmpList.AsEnumerable();


            #endregion
        }

        public static TaskMockupRepository Instance
        {
            get
            {
                return _instance;
            }
        }


        public IEnumerable<TaskEntity> GetTaskEntities(GetTaskDto taskDto)
        {

            IEnumerable<TaskEntity> result = 
                from taskEntity in DataSource select taskEntity;

            if(!string.IsNullOrEmpty(taskDto.Status))
            {
                result = (from taskEntity in result
                          where taskEntity.TaskStatus == taskDto.Status
                          select taskEntity);
            }

            if(taskDto.Limited != -1)
            {
                result = result.Take(taskDto.Limited);
            }

            if(taskDto.CreatedAfter.HasValue)
            {
                result = (from taskEntity in result
                          where taskEntity.CreatedDate > taskDto.CreatedAfter.Value
                          select taskEntity);
            }
            if (taskDto.CreatedBefore.HasValue)
            {
                result = (from taskEntity in result
                          where taskEntity.CreatedDate <= taskDto.CreatedAfter.Value
                          select taskEntity);
            }


            return result;

        }

        public string DeleteTaskEntity(string taskEntityId)
        {
            List<TaskEntity> currentList = DataSource.ToList();
            TaskEntity currentItem =
                (from item in DataSource where item.TaskId == taskEntityId select item).FirstOrDefault();

            if(currentItem != null)
            {
                currentList.Remove(currentItem);
            }

            DataSource = currentList.AsEnumerable();

            return string.Empty;
        }

        public TaskEntity FinishTaskEntity(string taskEntityId)
        {
            List<TaskEntity> currentList = DataSource.ToList();
            TaskEntity currentItem =
                (from item in DataSource where item.TaskId == taskEntityId select item).FirstOrDefault();

            if (currentItem != null)
            {
                currentItem.TaskStatus = "Finished";
            }

            DataSource = currentList.AsEnumerable();

            return currentItem;
        }

        public TaskEntity AddTaskEntity(string taskName, string taskStatus, bool isUrgent, bool isImportant)
        {
            
            // Update id
            string lastestId = "T_" + DataSource.Count() + 1;

            var newEntity = new TaskEntity(lastestId, taskName, taskStatus, isUrgent, isImportant, DateTime.Now);

            var newList = DataSource.ToList();

            newList.Add(newEntity);

            DataSource = newList.AsEnumerable();

            return newEntity;
        }
    }
}
